package com.hsd.znsh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hsd.znsh.config.ZnshMqttConfig;
import com.hsd.znsh.entity.Control;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.enums.AckEnum;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.properties.MqttProperties;
import com.hsd.znsh.repository.ControlRepository;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.service.ControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-02 14:41:51
 */
@Service
public class ControlServiceImpl extends BaseServiceImpl implements ControlService {

    @Autowired
    private ControlRepository controlRepository;

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private ZnshMqttConfig.MyGateway myGateway;

    @Autowired
    private MqttProperties mqttProperties;

    @Autowired
    private ExecutorService executorService;

    @Override
    public List<Control> getControlListByHostSn(String hostSn) {
        Host host = hostRepository.findByHostSn(hostSn);
        return controlRepository.findByHostId(host.getId());
    }

    @Override
    public void updateWorkStatus(String controlSn, Integer workStatus) {
        Control control = controlRepository.findByControlSn(controlSn);
        if(control.getOnlineStatus()!= OnlineStatusEnum.ONLINE.getCode()){
            throw new ZnshException("设备不在线，请上线之后再操作");
        }
        control.setWorkStatus(workStatus);
        controlRepository.save(control);
        if (workStatus == 0) {
            CompletableFuture.runAsync(()-> command(controlSn, "680304421403145916"),executorService);
        } else {
            CompletableFuture.runAsync(()-> command(controlSn, "680304430003003216"),executorService);
        }
    }

    private void command(String controlSn, String cmd) {
        String base64Cmd = Base64.getEncoder().encodeToString(Hex.decode(cmd));
        Map<String, Object> map = new HashMap<>();
        map.put("confirmed", true);
        map.put("fPort", 99);
        map.put("data", base64Cmd);
        String topic = "application/" + mqttProperties.getApplicationID() + "/node/" + controlSn + "/tx";
        int n=5;
        while (n>0) {
            myGateway.sendToMqtt(topic, JSONObject.toJSONString(map));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            n--;
        }
    }

    public static void test(){
        while (true){
            System.out.println(Base64.getEncoder().encodeToString(Hex.decode("680304430003003216")));
            return;
        }
    }

    public static void main(String[] args) {
        test();
    }

}
